<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="tong.li&#39;s blog">
  <meta name="keyword" content="彤哥哥博客，95后技术爱好者,现就职于同程旅行/同程艺龙上海分公司，专注于互联网技术分享的平台。">
  
    <link rel="shortcut icon" href="/css/images/icon.png">
  
  <title>
    
      使用iText库生成PDF报表 | 彤哥哥的博客
    
  </title>
  <link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/highlight.js/9.12.0/styles/tomorrow-night.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdn.staticfile.org/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>彤哥哥的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/series/" class="item-link">分类</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">项目</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/series/" class="menu-link">分类</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">项目</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>使用iText库生成PDF报表</h2>
  <p class="post-date">2021-12-31</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><p>最近项目需要做PDF版的报表，通过查询资料做PDF的JAVA类库有很多，比如iText、pdfbox、JasperReports等其他类库。最终选择iText5库进行报表生成。</p>
<p>本篇只是简单介绍了iText的基本API，更多高级用法请大家自行研究。</p>
<p>首先引入如下iText5.x依赖：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.itextpdf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>itextpdf<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.5.13<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.itextpdf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>itext-asian<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.2.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.commons<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-lang3<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>然后我们熟悉iText相关的API:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span>  com.lt.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.lt.pojo.BillEntity;</span><br><span class="line"><span class="keyword">import</span> com.itextpdf.text.*;</span><br><span class="line"><span class="keyword">import</span> com.itextpdf.text.pdf.BaseFont;</span><br><span class="line"><span class="keyword">import</span> com.itextpdf.text.pdf.PdfPCell;</span><br><span class="line"><span class="keyword">import</span> com.itextpdf.text.pdf.PdfPTable;</span><br><span class="line"><span class="keyword">import</span> com.itextpdf.text.pdf.PdfWriter;</span><br><span class="line"><span class="keyword">import</span> org.apache.commons.lang3.time.DateFormatUtils;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.CollectionUtils;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.ObjectUtils;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"><span class="keyword">import</span> java.text.DecimalFormat;</span><br><span class="line"><span class="keyword">import</span> java.time.LocalDate;</span><br><span class="line"><span class="keyword">import</span> java.time.ZoneId;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: com.lt.utils.PDFUtils</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: PDF工具类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 2021/12/31 20:35</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PDFUtils</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">static</span> <span class="type">Logger</span> <span class="variable">log</span> <span class="operator">=</span> LoggerFactory.getLogger(PDFUtils.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义全局标题的字体静态变量 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Font titlefont;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义全局头部的字体静态变量 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Font headfont;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义全局表头的字体静态变量 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Font keyfont;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 定义全局正文的字体静态变量 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Font textfont;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 表格最大宽度 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="variable">maxWidth</span> <span class="operator">=</span> <span class="number">520</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 时间格式化模板 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span>  <span class="keyword">static</span> <span class="type">String</span> <span class="variable">DATE_PATTERN</span> <span class="operator">=</span> <span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 默认格式化后的数字0 */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">defaultBigDecimalValue</span> <span class="operator">=</span> <span class="string">&quot;0.00&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 日期格式 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">DATE_PATTERN_YYYY_MM_DD</span> <span class="operator">=</span> <span class="string">&quot;yyyy/MM/dd&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 中式日期格式 */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">DATE_PATTERN_YYYY_MM_DD_CN</span> <span class="operator">=</span> <span class="string">&quot;yyyy年MM月dd日&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 静态初始化全局的字体变量</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 不同字体（这里定义为同一种字体：包含不同字号、不同style）</span></span><br><span class="line">            <span class="type">BaseFont</span> <span class="variable">bfChinese</span> <span class="operator">=</span> BaseFont.createFont(<span class="string">&quot;STSong-Light&quot;</span>, <span class="string">&quot;UniGB-UCS2-H&quot;</span>, BaseFont.NOT_EMBEDDED);</span><br><span class="line">            titlefont = <span class="keyword">new</span> <span class="title class_">Font</span>(bfChinese, <span class="number">16</span>, Font.BOLD);</span><br><span class="line">            headfont = <span class="keyword">new</span> <span class="title class_">Font</span>(bfChinese, <span class="number">10</span>, Font.BOLD);</span><br><span class="line">            keyfont = <span class="keyword">new</span> <span class="title class_">Font</span>(bfChinese, <span class="number">10</span>, Font.BOLD);</span><br><span class="line">            textfont = <span class="keyword">new</span> <span class="title class_">Font</span>(bfChinese, <span class="number">10</span>, Font.NORMAL);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            log.error(<span class="string">&quot;PDF字体初始化失败&quot;</span>,e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建空的PDF文件并设置相关属性</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> file   文档文件对象</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> title  文档标题</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> author 文档作者</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> IOException</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> DocumentException</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Document <span class="title function_">createPdfFile</span><span class="params">(File file, String title,  String author)</span></span><br><span class="line">            <span class="keyword">throws</span> IOException, DocumentException &#123;</span><br><span class="line">        <span class="comment">// 创建Document对象，并设置纸张大小为A4</span></span><br><span class="line">        <span class="type">Document</span> <span class="variable">document</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Document</span>(PageSize.A4);</span><br><span class="line">        file.createNewFile();</span><br><span class="line">        <span class="comment">// 获取PdfWriter实例，可以通过该实例可以设置文档属性，如水印、页眉、页脚等</span></span><br><span class="line">        <span class="type">PdfWriter</span> <span class="variable">writer</span> <span class="operator">=</span> PdfWriter.getInstance(document, <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(file));</span><br><span class="line">        <span class="comment">// 设置为PDF低版本</span></span><br><span class="line">        <span class="comment">//writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);</span></span><br><span class="line">        <span class="comment">// 设置文档属性</span></span><br><span class="line">        setDocumentAttribute(document, title, author);</span><br><span class="line">        <span class="keyword">return</span> document;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 设置PDF文档属性</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> document 文档对象</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> title    文档标题</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> author   文档作者</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">setDocumentAttribute</span><span class="params">(Document document,String title, String author)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (document == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">NullPointerException</span>(<span class="string">&quot;PDF Document instance is null&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 打开文档</span></span><br><span class="line">        <span class="keyword">if</span> (!document.isOpen()) &#123;</span><br><span class="line">            document.open();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 设置标题和主题</span></span><br><span class="line">        <span class="keyword">if</span> (!ObjectUtils.isEmpty(title)) &#123;</span><br><span class="line">            document.addTitle(title);</span><br><span class="line">            document.addSubject(title);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 设置作者和创建者</span></span><br><span class="line">        <span class="keyword">if</span> (!ObjectUtils.isEmpty(title)) &#123;</span><br><span class="line">            document.addAuthor(author);</span><br><span class="line">            document.addCreator(author);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 添加创建时间</span></span><br><span class="line">        document.addCreationDate();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建指定列宽、列数的表格</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> widths 宽度</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> PdfPTable <span class="title function_">createTable</span><span class="params">(<span class="type">float</span>[] widths)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (widths == <span class="literal">null</span> || widths.length == <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">PdfPTable</span> <span class="variable">table</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PdfPTable</span>(widths);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            table.setTotalWidth(maxWidth);</span><br><span class="line">            table.setLockedWidth(<span class="literal">true</span>);</span><br><span class="line">            table.setHorizontalAlignment(Element.ALIGN_CENTER);</span><br><span class="line">            table.getDefaultCell().setBorder(<span class="number">1</span>);</span><br><span class="line">            table.getDefaultCell().setMinimumHeight(<span class="number">25</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            log.error(<span class="string">&quot;PDF创建表格失败&quot;</span>,e);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> table;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建单元格(指定字体)</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> value  文本值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> font   字体</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> PdfPCell <span class="title function_">createCell</span><span class="params">(String value, Font font)</span> &#123;</span><br><span class="line">        <span class="type">PdfPCell</span> <span class="variable">cell</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PdfPCell</span>();</span><br><span class="line">        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);</span><br><span class="line">        cell.setHorizontalAlignment(Element.ALIGN_CENTER);</span><br><span class="line">        cell.setPhrase(<span class="keyword">new</span> <span class="title class_">Phrase</span>(value, font));</span><br><span class="line">        <span class="keyword">return</span> cell;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建单元格（指定字体、水平..）</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> value   文本值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> font    字体</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> align   对齐方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> PdfPCell <span class="title function_">createCell</span><span class="params">(String value, Font font, <span class="type">int</span> align)</span> &#123;</span><br><span class="line">        <span class="type">PdfPCell</span> <span class="variable">cell</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PdfPCell</span>();</span><br><span class="line">        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);</span><br><span class="line">        cell.setHorizontalAlignment(align);</span><br><span class="line">        cell.setPhrase(<span class="keyword">new</span> <span class="title class_">Phrase</span>(value, font));</span><br><span class="line">        <span class="keyword">return</span> cell;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> value     文本值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> font      字体</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> align     对齐方式</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> colspan   跨列</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> boderFlag 边界距离</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> PdfPCell <span class="title function_">createCell</span><span class="params">(String value, Font font, <span class="type">int</span> align, <span class="type">int</span> colspan, <span class="type">boolean</span> boderFlag)</span> &#123;</span><br><span class="line">        <span class="type">PdfPCell</span> <span class="variable">cell</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PdfPCell</span>();</span><br><span class="line">        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);</span><br><span class="line">        cell.setHorizontalAlignment(align);</span><br><span class="line">        cell.setColspan(colspan);</span><br><span class="line">        cell.setPhrase(<span class="keyword">new</span> <span class="title class_">Phrase</span>(value, font));</span><br><span class="line">        cell.setPadding(<span class="number">3.0f</span>);</span><br><span class="line">        cell.setBorder(<span class="number">0</span>);</span><br><span class="line">        cell.setPaddingTop(boderFlag ? <span class="number">0.0f</span> : <span class="number">15.0f</span>);</span><br><span class="line">        cell.setPaddingBottom(boderFlag ? <span class="number">15.0f</span> : <span class="number">8.0f</span>);</span><br><span class="line">        <span class="keyword">return</span> cell;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * BigDecimal格式化</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> value</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">formatBigDecimal</span><span class="params">(BigDecimal value)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (value == <span class="literal">null</span> || value.compareTo(BigDecimal.ZERO) == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> defaultBigDecimalValue;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">DecimalFormat</span> <span class="variable">df</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DecimalFormat</span>(defaultBigDecimalValue.concat(<span class="string">&quot;#&quot;</span>));</span><br><span class="line">        <span class="keyword">return</span> df.format(value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">generateBill</span><span class="params">(Document document, String title, String company, String startDate,</span></span><br><span class="line"><span class="params">                                          String endDate, List&lt;BillEntity&gt; data)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="type">Date</span> <span class="variable">date</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        <span class="type">String</span> <span class="variable">exportTime</span> <span class="operator">=</span> DateFormatUtils.format(date,DATE_PATTERN );</span><br><span class="line">        <span class="comment">// 段落</span></span><br><span class="line">        <span class="type">Paragraph</span> <span class="variable">paragraph</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Paragraph</span>(title, titlefont);</span><br><span class="line">        <span class="comment">//设置文字居中 0靠左   1，居中     2，靠右</span></span><br><span class="line">        paragraph.setAlignment(<span class="number">1</span>);</span><br><span class="line">        <span class="comment">//设置左缩进</span></span><br><span class="line">        paragraph.setIndentationLeft(<span class="number">12</span>);</span><br><span class="line">        <span class="comment">//设置右缩进</span></span><br><span class="line">        paragraph.setIndentationRight(<span class="number">12</span>);</span><br><span class="line">        <span class="comment">//设置首行缩进</span></span><br><span class="line">        paragraph.setFirstLineIndent(<span class="number">24</span>);</span><br><span class="line">        <span class="comment">//行间距</span></span><br><span class="line">        paragraph.setLeading(<span class="number">20f</span>);</span><br><span class="line">        <span class="comment">//设置段落上空白</span></span><br><span class="line">        paragraph.setSpacingBefore(<span class="number">5f</span>);</span><br><span class="line">        <span class="comment">//设置段落下空白</span></span><br><span class="line">        paragraph.setSpacingAfter(<span class="number">10f</span>);</span><br><span class="line">        <span class="comment">// 新建表格</span></span><br><span class="line">        <span class="type">PdfPTable</span> <span class="variable">table</span> <span class="operator">=</span> createTable(<span class="keyword">new</span> <span class="title class_">float</span>[]&#123;<span class="number">120</span>, <span class="number">120</span>, <span class="number">120</span>, <span class="number">120</span>, <span class="number">120</span>, <span class="number">120</span>, <span class="number">120</span>&#125;);</span><br><span class="line">        <span class="comment">// 计算总金额</span></span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">totalMoney</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (!CollectionUtils.isEmpty(data)) &#123;</span><br><span class="line">            totalMoney = data.stream().map(BillEntity::getRechargeTotalMoney).reduce(BigDecimal.ZERO,BigDecimal::add);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 写表头</span></span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;起始日期：&quot;</span> + startDate + <span class="string">&quot;               截止日期：&quot;</span> + endDate, headfont, Element.ALIGN_LEFT, <span class="number">9</span>, <span class="literal">false</span>));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;交易日期&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;微信充值金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;支付宝充值金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;充值总金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;微信退款金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;支付宝退款金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;退款总金额&quot;</span>, keyfont, Element.ALIGN_CENTER));</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj1</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj2</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj3</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj4</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj5</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="type">BigDecimal</span> <span class="variable">hj6</span> <span class="operator">=</span> BigDecimal.ZERO;</span><br><span class="line">        <span class="keyword">if</span> (!CollectionUtils.isEmpty(data)) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; data.size(); i++) &#123;</span><br><span class="line">                <span class="type">BillEntity</span> <span class="variable">billEntity</span> <span class="operator">=</span> data.get(i);</span><br><span class="line">                table.addCell(createCell(DateFormatUtils.format(billEntity.getBillDate(), DATE_PATTERN_YYYY_MM_DD), textfont));</span><br><span class="line">                hj1 = hj1.add(billEntity.getWxRechargeMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getWxRechargeMoney()), textfont));</span><br><span class="line">                hj2 = hj2.add(billEntity.getAliRechargeMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getAliRechargeMoney()), textfont));</span><br><span class="line">                hj3 = hj3.add(billEntity.getRechargeTotalMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getRechargeTotalMoney()), textfont));</span><br><span class="line">                hj4 = hj4.add(billEntity.getWxRefundMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getWxRefundMoney()), textfont));</span><br><span class="line">                hj5 = hj5.add(billEntity.getAliRefundMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getAliRefundMoney()), textfont));</span><br><span class="line">                hj6 = hj6.add(billEntity.getRefundTotalMoney());</span><br><span class="line">                table.addCell(createCell(formatBigDecimal(billEntity.getRefundTotalMoney()), textfont));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// --------------合计开始-----------------</span></span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;合计&quot;</span>, textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj1), textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj2), textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj3), textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj4), textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj5), textfont));</span><br><span class="line">        table.addCell(createCell(formatBigDecimal(hj6), textfont));</span><br><span class="line">        <span class="comment">// --------------合计结束-----------------</span></span><br><span class="line"></span><br><span class="line">        table.addCell(createCell(<span class="string">&quot;导出时间：&quot;</span> + exportTime, headfont, Element.ALIGN_LEFT, <span class="number">9</span>, <span class="literal">false</span>));</span><br><span class="line">        table.addCell(createCell(String.format(<span class="string">&quot;盖章：%s&quot;</span>,company)  ,</span><br><span class="line">                headfont, Element.ALIGN_RIGHT, <span class="number">9</span>, <span class="literal">false</span>));</span><br><span class="line">        table.addCell(createCell(DateFormatUtils.format(date,DATE_PATTERN_YYYY_MM_DD_CN),</span><br><span class="line">                headfont, Element.ALIGN_RIGHT, <span class="number">9</span>, <span class="literal">false</span>));</span><br><span class="line">        document.add(paragraph);</span><br><span class="line">        document.add(table);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 测试生成账单PDF文件</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> args</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        List&lt;BillEntity&gt; billPos = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="type">BillEntity</span> <span class="variable">bill</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">Random</span> <span class="variable">random</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            bill = <span class="keyword">new</span> <span class="title class_">BillEntity</span>();</span><br><span class="line">            random = <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">            bill.setBillDate(Date.from(LocalDate.of(<span class="number">2021</span>,<span class="number">12</span>,<span class="number">10</span> + i)</span><br><span class="line">                    .atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));</span><br><span class="line">            bill.setWxRechargeMoney(<span class="keyword">new</span> <span class="title class_">BigDecimal</span>(random.nextInt(<span class="number">100</span>)));</span><br><span class="line">            bill.setAliRechargeMoney(<span class="keyword">new</span> <span class="title class_">BigDecimal</span>(random.nextInt(<span class="number">100</span>)));</span><br><span class="line">            bill.setRechargeTotalMoney(bill.getWxRechargeMoney().add(bill.getAliRechargeMoney()));</span><br><span class="line">            bill.setWxRefundMoney(<span class="keyword">new</span> <span class="title class_">BigDecimal</span>(random.nextInt(<span class="number">100</span>)));</span><br><span class="line">            bill.setAliRefundMoney(<span class="keyword">new</span> <span class="title class_">BigDecimal</span>(random.nextInt(<span class="number">100</span>)));</span><br><span class="line">            bill.setRefundTotalMoney(bill.getWxRefundMoney().add(bill.getAliRefundMoney()));</span><br><span class="line">            bill.setCardTotalMoney(<span class="keyword">new</span> <span class="title class_">BigDecimal</span>(random.nextInt(<span class="number">100</span>)));</span><br><span class="line">            billPos.add(bill);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">Document</span> <span class="variable">pdfFile</span> <span class="operator">=</span> createPdfFile(<span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;D:\\自定义账单.pdf&quot;</span>),<span class="string">&quot;bill&quot;</span>,<span class="string">&quot;litong&quot;</span>);</span><br><span class="line">        generateBill(pdfFile,<span class="string">&quot;应用充值、退款金额汇总&quot;</span>,</span><br><span class="line">                <span class="string">&quot;彤哥哥信息技术有限公司&quot;</span>,<span class="string">&quot;20211210&quot;</span>,<span class="string">&quot;20211219&quot;</span>,billPos);</span><br><span class="line">        pdfFile.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lt.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.math.BigDecimal;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@className</span>: com.lt.utils.PDFUtils</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>: PDF工具类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: tong.li</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@createTime</span>: 202/12/31 20:33</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BillEntity</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Date billDate;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> BigDecimal rechargeTotalMoney;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> BigDecimal refundTotalMoney;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> BigDecimal cardTotalMoney;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 微信充值金额</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> BigDecimal wxRechargeMoney;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付宝充值金额</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> BigDecimal aliRechargeMoney;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 微信退款金额</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> BigDecimal wxRefundMoney;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 支付宝退款金额</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> BigDecimal aliRefundMoney;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最后，我们执行一下main()测试一下生成好的报表文件，文件预览如下：</p>
<p><img src="https://ltyeamin.github.io/imgs/2022/01/20220106161924.png"></p>
<p>怎么样，是不是这样的报表效果可以称得上“眉清目秀”？</p>
<p>我是热爱技术的彤哥哥，欢迎随时互动交流。</p>
<p>2021年已接近尾声，祝大家新年快乐!</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Java" >
    <span class="tag-code">Java</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2021/09/05/%E8%B7%9F%E7%9D%80%E5%BD%A4%E5%93%A5%E5%AD%A6%E7%BD%91%E7%BB%9C-%E4%BC%A0%E8%BE%93%E5%B1%82TCP&UDP%E5%8D%8F%E8%AE%AE/">
        <span class="nav-arrow">← </span>
        
          跟着彤哥学网络-传输层TCP&amp;UDP协议
        
      </a>
    
    
      <a class="nav-right" href="/2022/01/06/%E6%94%AF%E4%BB%98%E9%87%8D%E6%9E%84%E4%B9%8B%E8%B7%AF/">
        
          支付重构之路
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="ltyeamin/blogtalks"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="nav">none</ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://example.com/2021/12/31/使用iText库生成PDF报表/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2024 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a target="_blank" rel="noopener" href="https://github.com/ltyeamin">tong.li</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("https://cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("https://cdn.staticfile.org/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->



<script src='https://cdn.staticfile.org/mermaid/8.11.2/mermaid.min.js'></script>



<script src="/js/script.js"></script>


  </body>
</html>